<template>
  <div class="knowledge-card" @click="handleClick">
    <div class="cover" :class="{ gradient: item.FCover === 'gradient-bg' }">
      <img v-if="item.FCover !== 'gradient-bg'" :src="item.FCover" alt="cover" />
    </div>
    <div class="content">
      <div class="tag">{{ item.FTag }}</div>
      <div class="title">{{ item.FTitle }}</div>
      <div class="desc">{{ item.FDescription }}</div>
    </div>
    <div class="star" @click.stop="toggleStar">
      {{ localStarred ? "⭐" : "☆" }}
    </div>
    <div v-auth="'space:manage'" class="card-actions" @click.stop>
      <el-dropdown @command="handleCommand" trigger="click">
        <span class="el-dropdown-link">
          <el-icon><MoreFilled /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item v-for="action in actions" :key="action.label" :command="action.handler" :divided="action.divided">
              <el-icon v-if="action.icon"><component :is="action.icon" /></el-icon>
              {{ action.label }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from "vue";
import { MoreFilled, Edit, Delete, User } from "@element-plus/icons-vue";
import type { Space } from "@/api/interface/space";

// Props 类型声明
const props = defineProps<{
  item: Space.SpaceItem;
  actions?: Array<{
    label: string;
    handler: (item: Space.SpaceItem) => void;
    icon?: any;
    divided?: boolean;
  }>;
}>();

// Emits 类型声明
const emit = defineEmits<{
  (e: "cardClick", item: Space.SpaceItem): void;
  (e: "starChange", payload: { id: string; isStarred: boolean }): void;
  (e: "action", payload: { type: string; item: Space.SpaceItem }): void;
}>();

// 本地收藏状态
const localStarred = ref<boolean>(props.item.FIsStarred!);

// 默认操作菜单
const defaultActions = [
  {
    label: "编辑",
    handler: (item: Space.SpaceItem) => emit("action", { type: "edit", item }),
    icon: Edit
  },
  {
    label: "删除",
    handler: (item: Space.SpaceItem) => emit("action", { type: "delete", item }),
    icon: Delete,
    divided: true
  },
  {
    label: "设置权限",
    handler: (item: Space.SpaceItem) => emit("action", { type: "permission", item }),
    icon: User,
    divided: true
  }
];

// 合并传入的操作和默认操作
const actions = props.actions || defaultActions;

// 卡片点击事件
const handleClick = () => {
  emit("cardClick", props.item);
};

// 收藏切换
const toggleStar = () => {
  localStarred.value = !localStarred.value;
  emit("starChange", {
    id: props.item.FId,
    isStarred: localStarred.value
  });
};

// 处理操作菜单命令
const handleCommand = (command: (item: Space.SpaceItem) => void) => {
  command(props.item);
};

// 监听外部收藏状态变化
watch(
  () => props.item.FIsStarred,
  val => {
    localStarred.value = val!;
  }
);
</script>

<style scoped lang="scss">
.knowledge-card {
  position: relative;
  width: 160px;
  overflow: hidden;
  cursor: pointer;
  background: #ffffff;
  border-radius: 6px;
  box-shadow: 0 1px 6px rgb(0 0 0 / 10%);
  transition:
    transform 0.2s,
    box-shadow 0.2s;
  &:hover {
    box-shadow: 0 4px 12px rgb(0 0 0 / 15%);
    transform: translateY(-2px);
  }
}
.cover {
  height: 100px;
  overflow: hidden;
  background: #f0f0f0;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s;
  }
  &.gradient {
    background: linear-gradient(135deg, #e6e9f0 0%, #eef1f5 100%);
  }
}
.content {
  padding: 10px;
  .tag {
    margin-bottom: 4px;
    font-size: 12px;
    color: #409eff;
  }
  .title {
    margin-bottom: 4px;
    overflow: hidden;
    font-weight: bold;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .desc {
    display: -webkit-box;
    overflow: hidden;
    font-size: 12px;
    line-height: 1.5;
    color: #909399;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
}
.star {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 1;
  font-size: 18px;
  color: orange;
  cursor: pointer;
  user-select: none;
  transition: transform 0.2s;
  &:hover {
    transform: scale(1.2);
  }
}
.card-actions {
  position: absolute;
  top: 6px;
  left: 6px;
  z-index: 1;
  .el-dropdown-link {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    cursor: pointer;
    background: rgb(255 255 255 / 80%);
    border-radius: 4px;
    transition: background 0.2s;
    &:hover {
      background: rgb(255 255 255 / 100%);
    }
    .el-icon {
      font-size: 16px;
      color: #606266;
    }
  }
}
.el-dropdown-menu__item {
  display: flex;
  gap: 8px;
  align-items: center;
  .el-icon {
    font-size: 14px;
  }
}
</style>
